package com.ncst.mvcweb.controller;

import com.ncst.mvcweb.entity.User;
import com.ncst.mvcweb.service.UserService;
import com.ncst.mvcweb.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名和密码获取用户信息
     *
     * @param username 用户名
     * @param password 密码
     * @return 用户信息
     */
    @GetMapping(value = "/getOne")
    @ApiOperation(value = "根据用户名和密码获取用户信息", notes = "通过用户名和密码查询用户")
    public JsonResult getUser(
            @ApiParam(value = "用户名", required = true) @RequestParam String username,
            @ApiParam(value = "密码", required = true) @RequestParam String password) {
        log.info("【Controller】 username:" + username + " password:" + password);
        return new JsonResult<>("success","",userService.findByUsernameAndPassword(username, password));
    }

    /**
     * 获取所有用户列表
     * 请求路径/list?order=asc
     * @return 用户列表
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取所有用户列表", notes = "查询所有用户信息")
    public JsonResult list() {
        List<User> userList = userService.findAll();
        return new JsonResult<>("success", "", userList);
    }


    /**
     * 根据ID列表批量删除用户
     *
     * @param idList 用户ID列表
     * @return 操作结果
     */
    @PostMapping("deleteByList")
    @ApiOperation(value = "根据ID列表批量删除用户", notes = "删除指定ID列表的用户")
    public JsonResult deleteByList(
            @ApiParam(value = "用户ID列表", required = true) @RequestParam("idList[]") List<Integer> idList) {
        userService.delete(idList);
        return new JsonResult("success", "删除成功");
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("queryUserById")
    @ApiOperation(value = "根据ID查询用户信息", notes = "查询指定ID的用户信息")
    public JsonResult<User> queryUserById(
            @ApiParam(value = "用户ID", required = true)
            @RequestParam Integer id) {
        return new JsonResult<User>("success", "",userService.queryById(id));
    }

    /**
     * 保存用户信息
     *
     * @param user 用户信息
     * @return 操作结果
     */
    @PostMapping("save")
    @ApiOperation(value = "保存用户信息", notes = "新增或更新用户信息")
    public JsonResult save(
            @ApiParam(value = "用户信息", required = true)
            User user) {
        // 判断当前传入的用户的用户名是否已经存在
        User user1 = userService.findByUsername(user.getUsername());
        if (user1 != null) {
            return new JsonResult("fail", "用户名已经存在");
        }
        // 设置默认状态为正常
        if (user.getStatus() == null) {
            user.setStatus("0");
        }
        userService.save(user);
        return new JsonResult("success", "保存成功");
    }

    // 修改用户信息
    @PostMapping("update")
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    public JsonResult update(
            @ApiParam(value = "用户信息", required = true)
            User user) {
            userService.updateById(user);
            return new JsonResult("success", "修改成功");
        }

    /**
     * 启用用户
     *
     * @param id 用户ID
     * @return 操作结果
     */
    @PostMapping("enable")
    @ApiOperation(value = "启用用户", notes = "将用户状态设置为正常")
    public JsonResult enableUser(
            @ApiParam(value = "用户ID", required = true)
            @RequestParam Integer id) {
        User user = userService.queryById(id);
        if (user != null) {
            user.setStatus("0");
            userService.save(user);
            return new JsonResult("success", "用户已启用");
        }
        return new JsonResult("fail", "用户不存在");
    }

    /**
     * 禁用用户
     *
     * @param id 用户ID
     * @return 操作结果
     */
    @PostMapping("disable")
    @ApiOperation(value = "禁用用户", notes = "将用户状态设置为禁用")
    public JsonResult disableUser(
            @ApiParam(value = "用户ID", required = true)
            @RequestParam Integer id) {
        User user = userService.queryById(id);
        if (user != null) {
            user.setStatus("1");
            userService.save(user);
            return new JsonResult("success", "用户已禁用");
        }
        return new JsonResult("fail", "用户不存在");
    }

    /**
     * 批量启用用户
     *
     * @param idList 用户ID列表
     * @return 操作结果
     */
    @PostMapping("batchEnable")
    @ApiOperation(value = "批量启用用户", notes = "批量将用户状态设置为正常")
    public JsonResult batchEnableUser(
            @ApiParam(value = "用户ID列表", required = true)
            @RequestParam("idList[]") List<Integer> idList) {
        for (Integer id : idList) {
            User user = userService.queryById(id);
            if (user != null) {
                user.setStatus("0");
                userService.save(user);
            }
        }
        return new JsonResult("success", "批量启用成功");
    }

    /**
     * 批量禁用用户
     *
     * @param idList 用户ID列表
     * @return 操作结果
     */
    @PostMapping("batchDisable")
    @ApiOperation(value = "批量禁用用户", notes = "批量将用户状态设置为禁用")
    public JsonResult batchDisableUser(
            @ApiParam(value = "用户ID列表", required = true)
            @RequestParam("idList[]") List<Integer> idList) {
        for (Integer id : idList) {
            User user = userService.queryById(id);
            if (user != null) {
                user.setStatus("1");
                userService.save(user);
            }
        }
        return new JsonResult("success", "批量禁用成功");
    }
}
